﻿var select = function (el) {
    //data : a,b,c|1,2,3
    this._data = null;
    this._el = $(el);
    this._errorMsg = "";
    /***Append begin***/
    /*
    append data to select
    */
    this.Check = function () {
        if (this._data == null) {
            this._errorMsg = "数据源不允许为空";
            return false;
        }
        if (this._el.get(0).tagName != "SELECT") {
            this._errorMsg = "容器格式不对";
            return false;
        }
        return true;
    }

    this.Append = function (data) {
        this._data = data;
        if (this.Check()) {
            //append
            var _valArr = this._data.split('|')[1].split(',');
            var _textArr = this._data.split('|')[0].split(',');
            for (var i = 0; i < _textArr.length ; i++) {
                this._el.append($("<option value='" + _valArr[i] + "'>" + _textArr[i] + "</option>"));
            }
        }
    }
    /***Append end***/

    /*
    get select value
    */
    this.GetSelectVal = function () {
        if (!this.CheckOptionCount()) {
            return "999";
        }
        return this._el.val();
    }

    /*
    get select index
    */
    this.GetSelectIndex = function () {
        if (!this.CheckOptionCount()) {
            return "999";
        }
        return this._el.get(0).selectedIndex;
    }

    /**
    get select text
    */
    this.GetSelectText = function () {
        if (!this.CheckOptionCount()) {
            return "select里面没有数据";
        }
        var index = this.GetSelectIndex();
        return this._el.get(0).options[index].text;
    }

    /*
    check select's options is Exist
    */
    this.CheckOptionCount = function () {
        if (this._el.get(0).options.length == 0) {
            this._errorMsg = "该select里面没有数据";
            return false;
        }
        return true;
    }
}